/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "ChrModelManager.h"

#include "GDataSerializer.h"
#include "ChrModelArchive.h"

namespace GNgine
{
	const std::string ChrModelManager::PERSIS_FILE_NAME="ChrModel.gdt";

	//race/sex/model name and so on
	ChrModelManager::ChrModelManager()
	{
		GDataParser parser(PERSIS_FILE_NAME);
		if(!parser.success()){
			return;
		}

		GDataName gname=parser.getName();
		
		if(gname==GDATA_CHARACTER_MODEL){
			int size=parser.getParam()->getInt();
			_maleModelNames.resize(size);
			_femaleModelNames.resize(size);
			for(int i=0;i<size;i++){
				_raceNames.push_back(parser.getParam()->getString());

				int modelCnt=parser.getParam()->getInt();
				
				for(int j=0;j<modelCnt;j++){
					_maleModelNames[i].push_back(parser.getParam()->getString());
				}
				modelCnt=parser.getParam()->getInt();
				
				for(int j=0;j<modelCnt;j++){
					_femaleModelNames[i].push_back(parser.getParam()->getString());
				}
			}
		}
	}

	ChrModelManager& ChrModelManager::getSingleton()
	{
		static ChrModelManager inst;
		return inst;
	}

	int ChrModelManager::addChrRaceName(std::string raceName)
	{
		std::transform(raceName.begin(), raceName.end(),raceName.begin(), ::tolower);
		int index=getChrRaceId(raceName);
		if(index==-1){
			index=_raceNames.size();
			_raceNames.push_back(raceName);

			_maleModelNames.resize(_raceNames.size());
			_femaleModelNames.resize(_raceNames.size());
		}
		return index;
	}

	int ChrModelManager::getChrRaceId(std::string raceName)
	{
		std::transform(raceName.begin(), raceName.end(),raceName.begin(), ::tolower);
		int size=_raceNames.size();
		for(int i=0;i<size;i++){
			if(_raceNames[i]==raceName){
				return i;
			}
		}
		return -1;
	}


	void ChrModelManager::addChrModel(std::string raceName,std::string gender,std::string modelName)
	{
		std::transform(raceName.begin(), raceName.end(),raceName.begin(), ::tolower);
		std::transform(gender.begin(), gender.end(),gender.begin(), ::tolower);
		int race=getChrRaceId(raceName);
		bool male = (gender == "female") ? false : true;
		if(male){
			_maleModelNames[race].push_back(modelName);
		}else{
			_femaleModelNames[race].push_back(modelName);
		}

	}

	void ChrModelManager::addMaleChrModel(std::string raceName,std::string modelName)
	{
		std::transform(raceName.begin(), raceName.end(),raceName.begin(), ::tolower);
		int race=getChrRaceId(raceName);
		_maleModelNames[race].push_back(modelName);

	}

	void ChrModelManager::addFemaleChrModel(std::string raceName,std::string modelName)
	{
		std::transform(raceName.begin(), raceName.end(),raceName.begin(), ::tolower);
		int race=getChrRaceId(raceName);
		_femaleModelNames[race].push_back(modelName);

	}

	void ChrModelManager::serialize()
	{
		GDataGenerator gen(GDATA_CHARACTER_MODEL);
		int size=_raceNames.size();
		gen.addIntParam(size);
		for(int i=0;i<size;i++){
			gen.addStringParam(_raceNames[i]);
			
			int modelCnt=_maleModelNames[i].size();
			gen.addIntParam(modelCnt);
			for(int j=0;j<modelCnt;j++){
				gen.addStringParam(_maleModelNames[i][j]);
			}
			modelCnt=_femaleModelNames[i].size();
			gen.addIntParam(modelCnt);
			for(int j=0;j<modelCnt;j++){
				gen.addStringParam(_femaleModelNames[i][j]);
			}
			
		}

		gen.serialize(PERSIS_FILE_NAME);
		
	}
	//path in format race/male/*.m2
	bool ChrModelManager::modelFileExist(std::string path)
	{
		
		return GNgChrModelArchive.modelFileExist(path);
	}


}
